import Vue from 'vue'
import VueRouter from 'vue-router'
import routesConfig from '../js/config/routes-config'
import $N from 'nprogress'
import 'nprogress/nprogress.css'
import $S from '../js/common/storage'
import $GLOB from '../../globalConfig'
import $P from '../js/common/public'

// //基本页面
// import Home from "@/components/index/home"  //默认页面
// import Charts from "@/components/index/charts" //首页

// // // 系统设置
// import menuSet from "@/components/system/menu_set" //菜单管理
// import accountSet from "@/components/system/account_set" //账户管理
// import groupSet from "@/components/system/group_set" //分组管理
// import logSet from "@/components/system/log_set" //日志管理
// import Login from "@/components/login/index"
// import err_404 from "@/components/error/404"

// // app端
// import app_info from "@/components/app/index"

Vue.use(VueRouter)

// const routes = [
//     {
//         path: '/',
//         name: 'home',
//         // component: Home,
//         component: resolve => require(['@/components/index/home'], resolve),
//         meta: {
//             title: 'vue_element',
//             type: 'pc',
//         },
//         redirect: '/' + $S.Get('home'),
//         children: [
//             {
//                 path: 'charts',
//                 name: 'charts',
//                 // component: Charts,
//                 component: resolve => require(['@/components/index/charts'], resolve),
//                 meta: {
//                     title: '数据统计-' + $GLOB.LOGIN_TITLE,
//                     item_title: "数据统计",
//                     type: 'pc',
//                 }
//             },

//             // ===========================================  系统设置  ===============================
//             {
//                 path: 'menu_set',
//                 name: 'menu_set',
//                 // component: menuSet,
//                 component: resolve => require(['@/components/system/menu_set'], resolve),
//                 meta: {
//                     title: '菜单管理-' + $GLOB.LOGIN_TITLE,
//                     item_title: "菜单管理",
//                     type: 'pc',
//                 }
//             },
//             {
//                 path: 'account_set',
//                 name: 'account_set',
//                 // component: accountSet,
//                 component: resolve => require(['@/components/system/account_set'], resolve),
//                 meta: {
//                     title: '账户管理-' + $GLOB.LOGIN_TITLE,
//                     item_title: "账户管理",
//                     type: 'pc',
//                 }
//             },
//             {
//                 path: 'group_set',
//                 name: 'group_set',
//                 // component: groupSet,
//                 component: resolve => require(['@/components/system/group_set'], resolve),
//                 meta: {
//                     title: '分组管理-' + $GLOB.LOGIN_TITLE,
//                     item_title: "分组管理",
//                     type: 'pc',
//                 }
//             },
//             {
//                 path: 'log_set',
//                 name: 'log_set',
//                 // component: logSet,
//                 component: resolve => require(['@/components/system/log_set'], resolve),
//                 meta: {
//                     title: '日志管理-' + $GLOB.LOGIN_TITLE,
//                     item_title: "日志管理",
//                     type: 'pc',
//                 }
//             },
//         ],
//     },
//     {
//         path: '/login',
//         name: 'login',
//         // component: Login,
//         component: resolve => require(['@/components/login/index'], resolve),
//         meta: {
//             title: '登陆-' + $GLOB.LOGIN_TITLE,
//             type: 'pc',
//         }
//     },
//     {
//         path: '/404',
//         name: '404',
//         // component: err_404,
//         component: resolve => require(['@/components/error/404'], resolve),
//         meta: {
//             title: '页面不存在-' + $GLOB.LOGIN_TITLE,
//             type: 'pc',
//         }
//     }
// ];

const router = new VueRouter({
    ...routesConfig
    // routes,
});

router.beforeEach((to, from, next) => {
    $N.start();
    //页面 title 动态修改
    if (to.meta && to.meta.title) {
        document.title = to.meta.title;
    } else {
        document.title = $GLOB.LOGIN_TITLE;
    }
    if (to.meta.type == undefined || to.meta.type == '' || to.meta.type == null) {
        $N.done();
        next({ path: '/login' });
    }

    if (to.meta.type == 'app') {
        $N.done();
        next();
    }

    if (to.meta.type == 'pc') {
        $N.done();
        if (to.path == '/login' || to.path == '/404') {
            next();
        } else {
            const token = $S.Get('token') || '';
            if (token.length < 1) {
                $P.Msg('账号登录过期,请重新登录！', 3);
                next({
                    path: '/login'
                });
            } else {
                const menuList = $S.Get('menuList') || [];
                if (menuList.indexOf(to.path) != -1) {
                    next();
                } else {
                    next({
                        path: '/login'
                    });
                }

            }
        }
    }
});

router.afterEach((to, from, next) => {
    $N.done();
});


const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

export default router
